 
package com.farsunset.cim.client.android;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;


/**
 * 与服务端连接服务
 * @author 3979434
 *
 */
  public class CIMPushService extends Service {

   
    CIMConnectorManager manager;

    private IBinder binder=new CIMPushService.LocalBinder();
    
    @Override
    public void onCreate()
    {
    	manager = CIMConnectorManager.getManager(this.getApplicationContext());
    }
 
    @Override
    public int onStartCommand(Intent intent,int flag, int startId) {
    	
    	if(!manager.isConnected())
    	{
	    	if(intent!=null)
	    	{
		    	String host = intent.getStringExtra(CIMDataConfig.KEY_CIM_SERVIER_HOST);
		    	int port = intent.getIntExtra(CIMDataConfig.KEY_CIM_SERVIER_PORT, 28888);
		    	if(host!=null)
		    	{
		    	   manager.connect(host,port);
		    	}
	    	}else
	    	{
	    		String host = CIMDataConfig.getString(this, CIMDataConfig.KEY_CIM_SERVIER_HOST);
		    	int port =CIMDataConfig.getInt(this, CIMDataConfig.KEY_CIM_SERVIER_PORT);
		    	if(host!=null)
		    	{
		    	   manager.connect(host,port);
		    	}
	    	}
    	}
    	return Service.START_REDELIVER_INTENT;
    }
    
    public void connect(String host,int port)
    {
    	manager.connect(host,port);
    }

 
	@Override
	public IBinder onBind(Intent arg0) {
		return binder;
	}

    public class LocalBinder extends Binder{
    	
    	public CIMPushService getService()
    	{
            return CIMPushService.this;
        }
    }
}
